import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QGridLayout, QHBoxLayout, QTextEdit
from PyQt5.QtGui import QColor, QPalette
from PyQt5.QtCore import Qt

class ColorInfoDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.current_color = QColor(255, 128, 64, 200)  # 带透明度的橙色
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        
        self.color_display = QLabel()
        self.color_display.setMinimumHeight(100)
        
        self.info_text = QTextEdit()
        self.info_text.setReadOnly(True)
        self.info_text.setMaximumHeight(200)
        
        select_btn = QPushButton('选择颜色并查看信息')
        select_btn.clicked.connect(self.select_and_show_info)
        
        layout.addWidget(self.color_display)
        layout.addWidget(QLabel('颜色详细信息:'))
        layout.addWidget(self.info_text)
        layout.addWidget(select_btn)
        
        self.update_display()
        self.setLayout(layout)
        self.setWindowTitle('颜色信息演示')
        
    def select_and_show_info(self):
        from PyQt5.QtWidgets import QColorDialog
        
        color = QColorDialog.getColor(
            self.current_color,
            self,
            "选择颜色",
            QColorDialog.ShowAlphaChannel
        )
        
        if color.isValid():
            self.current_color = color
            self.update_display()
            
    def update_display(self):
        """更新颜色显示和详细信息"""
        color = self.current_color
        
        # 更新颜色显示
        brightness = (color.red() * 299 + color.green() * 587 + color.blue() * 114) / 1000
        text_color = "white" if brightness < 128 else "black"
        
        self.color_display.setStyleSheet(
            f"background-color: {color.name()}; "
            f"color: {text_color}; "
            f"border: 2px solid black; "
            f"font-weight: bold;"
        )
        self.color_display.setText(f"当前颜色: {color.name()}")
        self.color_display.setAlignment(Qt.AlignCenter)
        
        # 构建详细信息
        info = f"""=== RGB 颜色模型 ===
红色分量 (R): {color.red()} (0-255)
绿色分量 (G): {color.green()} (0-255)  
蓝色分量 (B): {color.blue()} (0-255)
Alpha通道 (A): {color.alpha()} (0-255, 0=完全透明)

=== HSV 颜色模型 ===
色相 (H): {color.hue()} (0-359)
饱和度 (S): {color.saturation()} (0-255)
明度 (V): {color.value()} (0-255)

=== CMYK 颜色模型 ===
青色 (C): {color.cyan()} (0-255)
洋红 (M): {color.magenta()} (0-255)
黄色 (Y): {color.yellow()} (0-255)
黑色 (K): {color.black()} (0-255)

=== 其他信息 ===
十六进制: {color.name()}
带Alpha的十六进制: {color.name(QColor.HexArgb)}
颜色是否有效: {color.isValid()}
颜色亮度: {brightness:.1f}
"""
        self.info_text.setPlainText(info)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ColorInfoDemo()
    window.show()
    sys.exit(app.exec_())
